import axios from 'axios'
import { ElMessage } from 'element-plus'
import {adminInfo} from "@/store/customer/inedx";

const service = axios.create({
    baseURL: import.meta.env.VITE_APP_BASE_API, // url = base url + request url
    timeout: 5000
  })
  

// Request interceptors
service.interceptors.request.use(
    (config) => {
    // Add X-Access-Token header to every request, you can add other custom headers here
      if (adminInfo.token) {
        config.headers['Authorization'] = 'Token '+adminInfo.token
      }
      return config
    },
    (error) => {
      Promise.reject(error)
    }
  )

  const codeList  = ['200','201','202','204',]

// Response interceptors
service.interceptors.response.use(
    (response) => {
        const res = response.data
        console.log(res)
        //登录成功
        if (codeList.indexOf(res.code) !== -1) {
            console.log(res)
            ElMessage({
                message: res.message || 'Error',
                type: 'error',
                duration: 5 * 1000
            })
            return Promise.reject(new Error(res.message || 'Error'))
        } else {
            return response.data
        }
    }
)

export default service